/**
 * @(#) ResupportMoneyProxy.as
 *
 * @date		2009. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	의료비 Proxy - Proxy
 * 				의료비를 조회를한다.
 */
package com.skt.common_hr.resupport.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.resupport.ResupportFacade;
	import com.skt.common_hr.resupport.model.vo.ResupportMoneyVO;
	import com.skt.common_hr.resupport.model.vo.ResupportSubTabVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class ResupportProxy extends CommonProxy
	{
		public static const NAME:String = "ResupportMoneyProxy";

		public static const LOAD_RESUPPORTMONEY_LIST_COMPLETE:String	= "LoadResupportMoneyListComplete";
		public static const LOAD_RESUPPORTMONEY_LIST_FAILED:String	= "LoadResupportMoneyListFailed";
		public static const LOAD_SINCHUNG_LIST_COMPLETE:String	= "LOAD_SINCHUNG_LIST_COMPLETE";
		public static const LOAD_SINCHUNG_LIST_FAILED:String	= "LOAD_SINCHUNG_LIST_FAILED";


		public var sGJAHR:String;
		public var mainList:ArrayCollection;
		public var subTabList:ArrayCollection; // 상세보기 한의료지원건수 에 대한 여러건의 의료비값

		public var detailVO:ResupportMoneyVO;
		public var detailSubVO:ResupportSubTabVO;

		public var key:String;
		public var printDataVO:ResupportMoneyVO;
		public var sinChungList:ArrayCollection;
		public var sortList:ArrayCollection;
		public var acMedSubject:ArrayCollection;
		public var acMedYear:ArrayCollection;
		public var printDataList:ArrayCollection;
		public var printSubTabData:ArrayCollection;
		
		
		//지원기준
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function ResupportProxy(data:Object=null)
		{
			super(NAME, data);
		}



		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		
		public function loadList(pernr:String, year:String=''):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_GJAHR"] = year;
			
			

			loadData("Z_HR_RE01", params, listResult, listFault);
		}
		
		//휴양소 지원금 신청시 기본사항 받아오기
		public function loadSave(GWAREKEY:String, pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_GWAREKEY"] = '';

			loadData("Z_HR_RE04", params, listResult2, listFault);
		}

		// 의료비 상세 조회
		public function loadDetail(gwarekey:String):void
		{
			var params:Object = new Object;
			params["I_GWAREKEY"] = gwarekey;
			params["I_PERNR"]	 = Variables.myPernr;
			
			loadData("Z_HR_RE04", params, detailResult, detailFault);
		}
		
		public function printData(gwarekey:String):void
		{
			var params:Object = new Object;
			params["I_GWAREKEY"] = gwarekey;
			params["I_PERNR"]	 = Variables.myPernr;
			
			loadData("Z_HR_RE04", params, printResult, printFault);
		}

		//의료비 입력
		public function save(I_GWAREKEY:String, tItabArr:Array, tSub_tabArr:Array):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] 	= I_GWAREKEY;

			var tables:Object = new Object();
			tables["ITAB"] = tItabArr;
			tables["SUB_TAB"] = tSub_tabArr;

			saveData("Z_HR_RE03", params, tables, saveResult, saveFault);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function deleteData(gwKey:String):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] = gwKey;

			loadData("Z_HR_RE02", params, deleteResult, deleteFault);
		}
		
		public function listResult(event:ResultEvent):void
		{
			if(retCode==0) {
				sGJAHR		= event.result.O_GJAHR;
				if(sGJAHR == '0000'){
					sGJAHR = '';
				}
				mainList 	= parseList(event.result.OTAB);		
				acMedSubject = parseCorsn2(event.result.T_CODE);
				acMedYear = parseCorsn3(event.result.T_GJAHR);		
				facade.sendNotification(ResupportProxy.LOAD_RESUPPORTMONEY_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(ResupportProxy.LOAD_RESUPPORTMONEY_LIST_FAILED, this);
			}
		}
		
		public function listResult2(event:ResultEvent):void
		{
			if(retCode==0) {
				sinChungList	= parseList2(event.result.OTAB);
				subTabList		= parseSelectedSubList(event.result.SUB_TAB);
				facade.sendNotification(ResupportFacade.LOAD_SINCHUNG_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(ResupportFacade.LOAD_SINCHUNG_LIST_FAILED, this);
			}
		}

		/**
		 * 의료비 신청 리스트가 실패 했을 때 처리
		 *
		 * @param
		 * @return void
		 */
		private function listFault(event:FaultEvent):void
		{
			facade.sendNotification(ResupportProxy.LOAD_RESUPPORTMONEY_LIST_FAILED, this);
		}

		/**
		 * 의료비 상세정보 조회
		 *
		 * @param
		 * @return void
		 */
		public function detailResult(event:ResultEvent):void
		{
			if(retCode==0 && event.result.OTAB.length>0) {
				detailVO 		= parseSelectedList(event.result.OTAB);
				subTabList		= parseSelectedSubList(event.result.SUB_TAB);
				facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_COMPLETE, this);
			} 
			else {
				detailVO = null;
				facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_FAILED, this);
			}
		}
		
		/**
		 * 경조금 신청 상세 정보 조회중 오류
		 *
		 * @param
		 * @return void
		 */
		private function detailFault(event:FaultEvent):void
		{
			detailVO = null;
			facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_FAILED, this);
		}
		
		private function parseCorsn2(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			retArr.addItem({label:'구분선택', data:-1});
			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["TEXT"], data:rec["CODE"]});
			}
			return retArr;
		}
		
		private function parseCorsn3(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["TEXT"], data:rec["CODE"]});
			}
			return retArr;
		}
		


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- 경조사입력
		 * @param
		 * @return void
		 */
		public function saveResult(event:ResultEvent):void
		{

			if(retCode==0) {
				key = event.result["O_GWAREKEY"];
				facade.sendNotification(ResupportFacade.SAVE_RESUPPORTMONEY_COMPLETE, this);
			}
			else {
				facade.sendNotification(ResupportFacade.SAVE_RESUPPORTMONEY_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			facade.sendNotification(ResupportFacade.SAVE_CELEBRATION_FAILED, this);
		}

		public function deleteResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(ResupportFacade.LOAD_CONGMONEY_DELETE_COMPLETE, this);
			}
			else {
				facade.sendNotification(ResupportFacade.LOAD_CONGMONEY_DELETE_FAILED, this);
			}
		}

		private function deleteFault(event:FaultEvent):void
		{
			facade.sendNotification(ResupportFacade.LOAD_CONGMONEY_DELETE_FAILED, this);
		}
		
		public function printResult(event:ResultEvent):void
		{
			if(retCode==0) {
				printDataList 		= parseList2(event.result.OTAB);
				printSubTabData		= parseSelectedSubList(event.result.SUB_TAB);
				facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_PRINT_COMPLETE, this);
			} 
			else {
				detailVO = null;
				facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_PRINT_FAILED, this);
			}
		}

		private function printFault(event:FaultEvent):void
		{
			facade.sendNotification(ResupportFacade.LOAD_CONGMONEY_DELETE_FAILED, this);
		}

		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 조회 현황 ArrayCollection
		 */
		private function parseList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in srcArr)
			{
				var vo:ResupportMoneyVO = new ResupportMoneyVO();

				vo.GWAREKEY		= rec["GWAREKEY"];		//Groupware 전자결재 Key
				vo.IDATE		= DateUtil.stringToDateTime(rec["IDATE"]);			//신청일
				vo.USEDT1		= DateUtil.stringToDateTime(rec["USEDT1"]);			//사용일
				vo.GJAHR		= rec["GJAHR"];
				vo.HOWMANY		= rec["HOWMANY"];		//지원건수
				vo.HOWMUCH		= rec["HOWMUCH"];		//지원금액
				vo.WSTATUS		= rec["WSTATUS"];		//결재진행상태
				vo.WSTATX		= rec["WSTATX"];		//결재진행상태텍스트
				
				// 변경삭제 유무 
				if(vo.WSTATUS == '1' || vo.WSTATUS == '4'){
					vo.WSTATUS_F = true;
				} else {
					vo.WSTATUS_F = false;
				}	
								
//				// 출력버튼 유무
//				if(vo.WSTATUS == '1' || vo.WSTATUS == '3'){
//					vo.WSTATUS_F2 = true;
//				} else {
//					vo.WSTATUS_F2 = false;
//				}

				retArr.addItem(vo);
			}
			return retArr;
		}
		
		private function parseList2(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in srcArr)
			{
				var vo:ResupportMoneyVO = new ResupportMoneyVO();

				vo.PERNR		= rec["PERNR"];
				vo.ENAME		= rec["ENAME"];
				vo.BETRG		= rec["BETRG"];
				vo.BETRG_Q		= rec["BETRG_Q"];
				vo.BETRG_Y		= rec["BETRG_Y"];
				vo.IDATE		= DateUtil.stringToDateTime(rec["IDATE"]);			//신청일
				vo.WSTATUS		= rec["WSTATUS"];
				vo.WSTATX		= rec["WSTATX"];
				vo.GJAHR		= rec["GJAHR"];
				vo.HOWMANY		= rec["HOWMANY"];
				vo.HOWMUCH		= rec["HOWMUCH"];		//지원금액
				vo.SBETRG		= rec["SBETRG"];		//지원금액
				
				vo.BANKA		= rec["BANKA"];
				vo.BANKN		= rec["BANKN"];
				vo.EMFTX		= rec["EMFTX"];
				
				vo.ORGTX		= rec["ORGTX"];
				

				retArr.addItem(vo);
			}
			return retArr;
		}
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 의료비 상세 조회 현황 ArrayCollection
		 */

		private function parseSelectedSubList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:ResupportSubTabVO = new ResupportSubTabVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "USEDT") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "BETRG" ) {
						retObj[field] = Math.round(obj[field]);
						trace('retObj[field] : ' + retObj[field].toString() + "obj[field] : " + obj[field].toString());
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}

		private function parseSelectedList(recArr:ArrayCollection):ResupportMoneyVO
		{
			var retObj:ResupportMoneyVO = new ResupportMoneyVO();
			for each(var obj:Object in recArr){
				for(var field:String in obj) {
					if(field == "HOWMUCH") {
						var cNum:Number = parseFloat(obj[field]);
						retObj[field] = cNum;
					}
					if(field == "IDATE") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else {
						retObj[field] = obj[field];
					}
					trace(obj[field]);
				}
			}
			return retObj;

			for each(var obj:Object in recArr){
				var vo:ResupportMoneyVO = new ResupportMoneyVO();
			}
			return vo;
		}

	}
}